<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class PromotersController extends AdminbaseController {

    protected $promotersModel;
    protected $promotion_registrationModel;
    public function _initialize(){
        parent::_initialize();
        $this->promotersModel = M("promoters");
        $this->promotion_registrationModel = M("promotion_registration");
    }

    //查询列表
    public function index(){
        $post = I('');

        $keyword = trim($post['keyword']);
        $status = $post['status'];

        $this->assign('keyword',$keyword);
        $this->assign('status',$status);

        if($keyword){
            $where['phone_mob|user_name'] = array("like","%$keyword%");
        }
        if($status){
            $where['status'] = $status;
        }

        $p = $post['p']?$post['p']:1;
        $pageSize = 20;
        $where['delete_time'] = 0;
        $count = $this->promotersModel->where($where)->count();
        $list  = $this->promotersModel->where($where)->limit(($p-1)*$pageSize,$pageSize)->order("create_time desc")->select();

        $page = $this->page($count,$pageSize);
        $this->assign('page',$page->show("Admin"));
        $this->assign('list',$list);
        $this->display();

    }

    //添加
    public function add(){
        if(IS_POST){
            $postData = I("post.");
            if(!$postData['user_name'] || !$postData['phone_mob']){
                echo json_encode(array("code"=>-1,'msg'=>'参数错误'));
                exit();
            }
            if(!$this->check_mobile($postData['phone_mob'])){
                echo json_encode(array("code"=>-1,'msg'=>'手机号格式错误'));
                exit();
            }
            $memberModel = M("member");
            $memberInfo = $memberModel->where(array('phone_mob'=>$postData['phone_mob']))->find();
            if(!$memberInfo){
                echo json_encode(array('code'=>-1,'msg'=>'手机号输入错误，未查到用户！'));
                exit();
            }
            $promotersInfo = $this->promotersModel->where(array("phone_mob"=>$postData['phone_mob']))->find();
            if($promotersInfo){
                echo json_encode(array('code'=>"-1",'msg'=>'手机号已被占用，请填写其他的手机号！'));
                exit();
            }
            $addData['user_id']   = $memberInfo['user_id'];
            $addData['phone_mob'] = $postData['phone_mob'];
            $addData['user_name'] = $postData['user_name'];
            $addData['post']      = $postData['post'];
            $addData['create_time'] = time();

            $addInfo = $this->promotersModel->add($addData);
            if($addInfo){
                echo json_encode(array('code'=>1,'msg'=>'添加成功！'));
                exit();
            }
            echo json_encode(array('code'=>-1,'msg'=>"添加失败！"));
            exit();
        }else{
            $this->display();
        }
    }

    //编辑
    public function edit(){
        if(IS_POST){
            $postData = I("post.");
            if(!$postData['user_name'] || !$postData['phone_mob']){
                echo json_encode(array("code"=>-1,'msg'=>'参数错误'));
                exit();
            }
            if(!$this->check_mobile($postData['phone_mob'])){
                echo json_encode(array("code"=>-1,'msg'=>'手机号格式错误'));
                exit();
            }
            $memberModel = M("member");
            $memberInfo = $memberModel->where(array('phone_mob'=>$postData['phone_mob']))->find();
            if(!$memberInfo){
                echo json_encode(array('code'=>-1,'手机号输入错误，未查到用户！'));
                exit();
            }
            $promotersInfo = $this->promotersModel->where(array("phone_mob"=>$postData['phone_mob'],'id'=>array('neq',$postData['id'])))->find();
            if($promotersInfo){
                echo json_encode(array('code'=>"-1",'msg'=>'手机号已被占用，请填写其他的手机号！'));
                exit();
            }
            $addData['user_id']   = $memberInfo['user_id'];
            $addData['phone_mob'] = $postData['phone_mob'];
            $addData['user_name'] = $postData['user_name'];
            $addData['post']      = $postData['post'];
            $addData['update_time'] = time();

            $addInfo = $this->promotersModel->where(array('id'=>$postData['id']))->save($addData);
            if($addInfo){
                echo json_encode(array('code'=>1,'msg'=>'保存成功！'));
                exit();
            }
            echo json_encode(array('code'=>-1,'msg'=>"保存失败！"));
            exit();
        }else{
            $id = I("get.id",0,'intval');
            if(!$id){
                $this->error('网络信息错误！');
            }
            $info = $this->promotersModel->where(array("id"=>$id))->find();
            $this->assign('info',$info);
            $this->display();
        }
    }

    //删除
    public function delete(){
        $id = I("post.id");
        $code = -1;
        $msg = '网络错误';
        if($id){
            $delete = $this->promotersModel->where(array("id"=>$id))->save(array("delete_time"=>time()));
            $msg = '删除失败';
            if($delete){
                $code = 1;
                $msg = '删除成功';
            }
        }
        $arr['code'] = $code;
        $arr['msg'] = $msg;
        echo json_encode($arr);
        exit();
    }

    //状态操作
    public function updateStatus(){
        $id = I("post.id");
        $status = I("post.status");
        $code = -1;
        $msg = '参数错误';
        if($id && $status){
            $where['id'] = $id;
            $saveData['status'] = $status;
            $save = $this->promotersModel->where($where)->save($saveData);
            $msg = '修改失败';
            if($save){
                $code = 1;
                $msg = '修改成功！';
            }
        }
        $arr['code'] = $code;
        $arr['msg'] = $msg;
        echo json_encode($arr);
        exit();
    }

    //查看推广人的邀请信息
    public function statistics(){
        $user_id = I('get.id');

        $info = $this->promotersModel->where(array("user_id"=>$user_id))->find();
        if(!$info){
            $this->error('工作人员不存在！');
        }

        $where['phone'] = $info['phone_mob'];
        //查看邀请次数
        $count = $this->promotion_registrationModel
            ->where($where)->order('id desc')->count();
        $promotionRegistrationInfo = $this->promotion_registrationModel
            ->where($where)->order('id desc')->select();

        $time = time();
        $num = 0;
        foreach($promotionRegistrationInfo as $k=>$v){
            $promotionRegistrationInfo[$k]['start_time'] = $v['end_time'] - 60*60*24;
            if($v['end_time'] > $time && $v['status'] == 1){
                $promotionRegistrationInfo[$k]['status_value'] = '未注册';
            }elseif($v['status'] == 2){
                $promotionRegistrationInfo[$k]['status_value'] = '已注册';
            }else{
                $promotionRegistrationInfo[$k]['status_value'] = '已失效';
            }
            if($v['create_time'] == 0){
                $promotionRegistrationInfo[$k]['time_value'] = '无';
            }else{
                $promotionRegistrationInfo[$k]['time_value'] = date('Y-m-d H:i:s',$v['create_time']);
            }
            if($v['status'] == 2){
                $num ++;
            }
        }

        //邀请成功率
        $invited_bibi = (sprintf("%.2f",$num / $count) * 100).'%';

        $this->assign('count',$count);
        $this->assign('num',$num);
        $this->assign('invited_bibi',$invited_bibi);
        $this->assign('list',$promotionRegistrationInfo);
        $this->display();
    }

    //查看推广人的推广情况
    public function memberlist(){
        $store_id = I("get.store_id");
        if(!$store_id){
            $this->error('信息传递错误!');
        }
        $where['fourth_class'] = $store_id;
        $list = $this->memberListWhere($where,'reg_time desc');

        $info = D("member")->where(array("user_id"=>$store_id))->find();
        $this->assign('info',$info);
        $this->assign('list',$list);
        $this->assign('total',count($list));
        $this->display();
    }


}